<?php

namespace model;

/**
 * Model Danych odpowiadający za kategorie
 *
 * @author Damian
 */
class TblCategory extends Model {
    /**
     * @readwrite
     */
    public $itemCount;
    /**
     * @readwrite
     * @column
     * @primary
     * @type int 
     * @length 11
     */
    public $CategoryID;

    const ID = "CategoryID";

    /**
     * @readwrite
     * @column
     * @type varchar
     * @lenght 100
     * @validate required, alpha, min(3), max(50)
     */
    public $Name;

    const NAME = "Name";

    /**
     * @readwrite
     * @column
     * @isObject
     * @type TblCategory
     */
    public $ParentCategoryID;

    const PARENT_CATEGORY_ID = "ParentCategoryID";

    /**
     * @readwrite
     * @column
     * @isObject
     * @type TblShop
     */
    public $ShopID;

    const SHOP_ID = "ShopID";

    /**
     * @column
     * @type text
     * @readwrite
     * @validate required, alpha
     */
    public $Description;

    const DESCRIPTION = "Description";

    /**
     * @readwrite
     */
    public $Depth; //zagnieżdżenie kategorii

    const DEPTH = "Depth";

    /**
     * @readwrite
     */
    public $HasChildren;

    const HAS_CHILDREN = "HasChildren";

    /**
     * @readwrite
     * @column
     * @type text
     */
    public $URL;

    const URL = "URL";

    /**
     *
     * @var array <p>lista kategorii rodziców</p>
     */
    public $breadCramps = false;

    /**
     * <p>funkcja tworzy łańczueszk kategorii do kategorii głównej</p>
     * @return array
     */
    public function createBreadCramps() {
        if (!$this->breadCramps) {
            $parentCategoryCriteria = new Criteria();
            $parentCategoryCriteria->from(TblCategory::getModel()->getTable())->where(TblCategory::ID, "=", $this->ParentCategoryID);
            $parent = TblCategory::getModel()->first($parentCategoryCriteria);
            //var_dump($parent);
//            $result = array();
            $i = 0;
            $result[$i]["Name"] = $this->Name;
            $result[$i]["ID"] = $this->CategoryID;
            $result[$i]["Description"] = strip_tags($this->Description);
            $result[$i]["URL"] = $this->URL;
            $result[$i]["Active"]=false;
            while (is_object($parent) && $parent->CategoryID!=null) {
                $i++;
                $result[$i]["Name"] = $parent->Name;
                $result[$i]["ID"] = $parent->CategoryID;
                $result[$i]["Description"] = strip_tags($parent->Description);
                $result[$i]["URL"] = $parent->URL;
                $result[$i]["Active"]=false;
                $criteria = new Criteria();
                $criteria->from(TblCategory::getModel()->getTable())->where(TblCategory::ID, "=", $parent->ParentCategoryID);
                $parent = TblCategory::getModel()->first($criteria);
            }
            //odwrócenie kolejności
            $keys = array_keys($result);
            $newResult = array();
            for ($j = 0; $j < sizeof($keys); $j++) {
                $newResult[$j] = $result[$keys[sizeof($keys) - $j - 1]];
                if ($j==sizeof($keys)-1) {
                    $newResult[$j]["Active"]=true;
                }
            }
            $this->breadCramps = $newResult;
        }
        return $this->breadCramps;
    }

    /**
     * <p>zwraca listę kategorii dzieci dla danej</p>
     * @return TblCategory[]
     */
    public function getChildren() {
        $ChildrenCriteria = new Criteria();
        $ChildrenCriteria->from(TblCategory::getModel()->getTable())->where(TblCategory::PARENT_CATEGORY_ID, "=", $this->CategoryID);
        return TblCategory::getModel()->all($ChildrenCriteria);
    }
    /**
     * <p>pobiera listę wszystkich id kategorii aktualnej oraz jej wszystkich potomków</p>
     * @return array
     */
    public function getAllIds() {
        $result = array();
        $result[$this->CategoryID] = $this->CategoryID;
        $categoriesCriteria = new Criteria();
        $categoriesCriteria->from(TblCategory::getModel()->getTable())->where(\model\TblCategory::PARENT_CATEGORY_ID, "=", $this->CategoryID);
        $categories = \model\TblCategory::getModel()->all($categoriesCriteria);
        if ($categories) {
            foreach ($categories as $category) {
                if ($category->HasChildren) {
                    $result+=$category->getAllIds();
                } else {
                    $result[$category->CategoryID] = $category->CategoryID;
                }
            }
        }
        return $result;
    }

    /**
     * <p>sprawdza czy dany element jest w danym sklepie</p>
     * @param \model\TblShop $shop <p>sklep do sprawdzenia</p>
     * @return boolean
     */
    public function compare(TblShop &$shop) {
        $myshop = TblShop::getModel()->getByPrimaryKey($this->ShopID);
        if (!$myshop) {
            return false;
        }
        return $myshop->ShopID == $shop->ShopID;
    }

    public function generateURL() {
        $this->URL = strtolower(str_replace([" ", "-"], "_", strip_tags($this->Name)));
    }
     /**
     * 
     * @param type $shopID <p>id sklepu w którym szukamy</p>
      * @param boolean $withChildren [optional=true] Określa czy pobierać wszystkie kategorie czy tylko te bez dzieci
     * @return TblCategory[] <p>tablica kategorii z danego sklepu</p>
     */
    public function getAllByShopID($shopID,$withChildren=true){
        $criteria = new Criteria();
        $criteria->from(\model\TblCategory::getModel()->getTable())->where(\model\TblManufacturer::SHOP_ID, "=", $shopID);
        if (!$withChildren){
            $criteria->where(TblCategory::HAS_CHILDREN, "=", 0,  TblCategory::getModel()->getTable(), " and ");
        }
        return $this->all($criteria);
    }
    /**
     * 
     * @param type $URL <p>url kategorii</p>
     * @param type $shopID <p>Id sklepu</p>
     * @return TblCategory
     */
    public function getByURL($URL, $shopID){
        $criteria = new Criteria();
        $criteria->from(\model\TblCategory::getModel()->getTable())
                ->where(\model\TblCategory::URL, "=", $URL)->where(\model\TblCategory::SHOP_ID, "=", $shopID, false, " and ");
        return $this->first($criteria);
    }

}
